# -*- coding: utf-8 -*-
# @Time    : 2018/3/12 1:20
# @Author  : Soft
# @File    : service.py
# @Desc    : 自习记录service
import logging
from module.models import z_study, z_study_record, db, Page
from module.TimeUtils import get_date, get_time, zs_limit

log = logging.getLogger('Z-STUDY SERVICE LOG')

class Z_StudyService:
    def __init__(self):
        pass

    @staticmethod
    @db.atomic()
    def add(**record):
        return z_study_record.insert(**record).execute()

    @staticmethod
    @db.atomic()
    def remove(wid=0):
        return z_study_record.delete_by_id(pk=wid)

    @staticmethod
    def change(wid=0, **record):
        try:
            with db.atomic():
                z_study_record.update(**record).where(z_study_record.id == wid).execute()
                return True
        except BaseException as e:
            log.debug('修改失败--> %s' % e)
            return False

    @staticmethod
    def is_has(stu_id):
        date = get_date()
        try:
            result = z_study_record.select(). \
                where(z_study_record.stu == stu_id, z_study_record.datetime.startswith(date)).get()
            return result
        except BaseException as e:
            log.debug('不存在记录--> %s' % e)
            return False

    @staticmethod
    def query(page=1, size=12, start='2018-03-12', end='2025-03-12', stu_id=None, status=None):
        if stu_id:
            result = z_study_record.select(). \
                where(z_study_record.stu == stu_id, z_study_record.datetime >= start,
                      z_study_record.datetime <= end). \
                order_by(z_study_record.datetime.desc()). \
                paginate(page, size)
            Page.total = z_study_record.select(). \
                where(z_study_record.stu == stu_id, z_study_record.datetime >= start,
                      z_study_record.datetime <= end).count()
        else:
            result = z_study_record.select(). \
                where(z_study_record.datetime >= start, z_study_record.datetime <= end). \
                order_by(z_study_record.datetime.desc()). \
                paginate(page, size)
            Page.total = z_study_record.select(). \
                where(z_study_record.datetime >= start, z_study_record.datetime <= end).count()
        Page.result = []
        for re in result:
            try:
                if status and int(status) >= 0:
                    if re.rd_status == int(status):
                        Page.result.append(re.to_dict())
                else:
                    Page.result.append(re.to_dict())
            except BaseException as e:
                log.debug('记录不存在--> %s' % e)
                Page.total -= 1
                continue
        Page.page = page
        Page.limit = size
        return Page.to_dict()

    @staticmethod
    def get_z_study_info():
        result = []
        for zs in z_study.select():
            result.append(zs.to_dict())
        return result

    @staticmethod
    def can_check():
        time = get_time(30)[-5:]
        zs = z_study.select().where(z_study.start_time < time)
        if len(zs) > 0:
            start_time = get_date() + " " + zs[0].start_time
            end_time   = get_date() + " " + zs[0].end_time
            limit = zs_limit(start_time, end_time)
            return limit
        return False

# print Z_StudyService.get_z_study_info()
# print Z_StudyService.can_check()
